package com.bsh.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class HttpClientUtils {
	@SuppressWarnings("finally")
	public static String executeGet(String url) throws Exception {
		BufferedReader in = null;

		String content = null;
		try {
			// 定义HttpClient
			HttpClient client = new DefaultHttpClient();
			// 实例化HTTP方法
			HttpGet request = new HttpGet();
			request.setURI(new URI(url));
			HttpResponse response = client.execute(request);
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			content = sb.toString();
		} finally {
			if (in != null) {
				try {
					in.close();// 最后要关闭BufferedReader
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return content;
		}
	}

//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public static String executeHttpsGet(String url) {
//		String responseBody = null;
//		try {
//
//			HttpClient httpclient = new DefaultHttpClient();
//			SSLContext ctx = SSLContext.getInstance("SSL");
//			X509TrustManager tm = new X509TrustManager() {
//				public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
//
//				}
//
//				public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
//				}
//
//				public X509Certificate[] getAcceptedIssuers() {
//					return null;
//				}
//			};
//			ctx.init(null, new TrustManager[] { tm }, null);
//			SSLSocketFactory ssf = new SSLSocketFactory(ctx);
//			ClientConnectionManager ccm = httpclient.getConnectionManager();
//			SchemeRegistry sr = ccm.getSchemeRegistry();
//			sr.register(new Scheme("https", 443, ssf));
//			HttpGet httpget = new HttpGet(url);
//			HttpParams params = httpclient.getParams();
//			params.setParameter("param1", "paramValue1");
//			httpget.setParams(params);
//			System.out.println("REQUEST:" + httpget.getURI());
//			
//			ResponseHandler responseHandler = new BasicResponseHandler();
//			responseBody = httpclient.execute(httpget, responseHandler);
//		} catch (Exception ex) {
//			ex.printStackTrace();
//		}
//
//		return responseBody;
//	}

	public static String executePost(String url, JSONObject jsonParam) {
		HttpClient client = new DefaultHttpClient();
		HttpPost method = new HttpPost(url);
		StringEntity entity;
		String result = null;
		try {
			entity = new StringEntity(jsonParam.toString(), "utf-8");
			// 解决中文乱码问题
			entity.setContentEncoding("UTF-8");
			entity.setContentType("application/json");
			method.setEntity(entity);
			HttpResponse response = client.execute(method);
			result = EntityUtils.toString(response.getEntity(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// XC modified on 2016/06/11 for release net resources
			method.releaseConnection();
			((BasicClientConnectionManager) client.getConnectionManager()).shutdown();
		}

		return result;

	}

	public static void main(String args[]) {

		try {
			String result=HttpClientUtils.executeGet("http://api.id98.cn/api/idcard?appkey=d10a8e06284cf889deaf93ffb5d9c60a&name=邓永望&cardno=610922197401232578");
		    System.out.println(result);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
